
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第一节 输入手写数字图片输出识别结果 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="section4.2.html" />
    
    
    <link rel="prev" href="./" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/section2.1.html">
            
                <a href="../chapter2/section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/section2.2.html">
            
                <a href="../chapter2/section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/section2.3.html">
            
                <a href="../chapter2/section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/section2.4.html">
            
                <a href="../chapter2/section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/section2.5.html">
            
                <a href="../chapter2/section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.5.1" data-path="section4.1.html">
            
                <a href="section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="section4.2.html">
            
                <a href="section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第一节 输入手写数字图片输出识别结果</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E00;&#x8282;-&#x8F93;&#x5165;&#x624B;&#x5199;&#x6570;&#x5B57;&#x56FE;&#x7247;&#x8F93;&#x51FA;&#x8BC6;&#x522B;&#x7ED3;&#x679C;">&#x7B2C;&#x4E00;&#x8282; &#x8F93;&#x5165;&#x624B;&#x5199;&#x6570;&#x5B57;&#x56FE;&#x7247;&#x8F93;&#x51FA;&#x8BC6;&#x522B;&#x7ED3;&#x679C;</h1>
<h2 id="&#x65AD;&#x70B9;&#x7EED;&#x8BAD;">&#x65AD;&#x70B9;&#x7EED;&#x8BAD;</h2>
<ul>
<li>&#x5173;&#x952E;&#x5904;&#x7406;&#xFF1A;&#x52A0;&#x5165;<code>ckpt</code>&#x64CD;&#x4F5C;&#xFF1A;</li>
</ul>
<pre><code class="lang-python">ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
<span class="hljs-keyword">if</span> ckpt <span class="hljs-keyword">and</span> ckpt.model_checkpoint_path:
  saver.restore(sess, ckpt.model_checkpoint_path)
</code></pre>
<ol>
<li>&#x6CE8;&#x89E3;&#xFF1A;</li>
</ol>
<p>1)<code>tf.train.get_checkpoint_state(checkpoint_dir,latest_filename=None)</code>&#x8BE5;&#x51FD;&#x6570;&#x8868;&#x793A;&#x5982;&#x679C;&#x65AD;&#x70B9;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x5305;&#x542B;&#x6709;&#x6548;&#x65AD;&#x70B9;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x8BE5;&#x6587;&#x4EF6;&#x3002;
&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;
<code>checkpoint_dir</code>&#xFF1A;&#x8868;&#x793A;&#x5B58;&#x50A8;&#x65AD;&#x70B9;&#x6587;&#x4EF6;&#x7684;&#x76EE;&#x5F55;
<code>latest_filename=None</code>&#xFF1A;&#x65AD;&#x70B9;&#x6587;&#x4EF6;&#x7684;&#x53EF;&#x9009;&#x540D;&#x79F0;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;<code>checkpoint</code></p>
<p>2)<code>saver.restore(sess, ckpt.model_checkpoint_path)</code>&#x8BE5;&#x51FD;&#x6570;&#x8868;&#x793A;&#x6062;&#x590D;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#xFF0C;&#x5C06;<code>ckpt</code>&#x4E2D;&#x7684;&#x503C;&#x8D4B;&#x7ED9;<code>w</code> &#x548C;<code>b</code>&#x3002;
&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;
<code>sess</code>&#xFF1A;&#x8868;&#x793A;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#xFF0C;&#x4E4B;&#x524D;&#x4FDD;&#x5B58;&#x7684;&#x7ED3;&#x679C;&#x5C06;&#x88AB;&#x52A0;&#x8F7D;&#x5165;&#x8FD9;&#x4E2A;&#x4F1A;&#x8BDD;
<code>ckpt.model_checkpoint_path</code>&#xFF1A;&#x8868;&#x793A;&#x6A21;&#x578B;&#x5B58;&#x50A8;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x6A21;&#x578B;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5B83;&#x4F1A;&#x53BB;&#x67E5;&#x770B;<code>checkpoint</code>&#x6587;&#x4EF6;&#xFF0C;&#x770B;&#x770B;&#x6700;&#x65B0;&#x7684;&#x662F;&#x8C01;&#xFF0C;&#x53EB;&#x505A;&#x4EC0;&#x4E48;&#x3002;</p>
<ol>
<li><code>ckpt</code>&#x4EE3;&#x7801;&#x4F4D;&#x7F6E;&#xFF1A;</li>
</ol>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  init_op = tf.global_variables_initializer()
  sess.run(init_op)

  ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
  <span class="hljs-keyword">if</span> ckpt <span class="hljs-keyword">and</span> ckpt.model.checkopoint_path:
    saver.restore(sess, ckpt.model_checkpoint_path)

    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(STEPS):
      xs, ys = mnist.train.next_batch(BATCH_SIZE)
      _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict:{x: xs, y_: ys})
      <span class="hljs-keyword">if</span> i % <span class="hljs-number">1000</span> == <span class="hljs-number">0</span>:
        print(<span class="hljs-string">&quot;After %d training step(s), loss on training batch is %g.&quot;</span> % (step, loss_value))
        saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
</code></pre>
<ol>
<li>&#x5B9E;&#x8DF5;&#x4EE3;&#x7801;&#x9A8C;&#x8BC1;&#xFF1A;</li>
</ol>
<pre><code>$ python mnist_backward.py
Extracting ./data/train-images-idx3-ubyte.gz
Extracting ./data/train-labels-idx1-ubyte.gz
Extracting ./data/t10k-images-idx3-ubyte.gz
Extracting ./data/t10k-labels-idx1-ubyte.gz
After 50003 training step(s), loss on training batch is 0.127414.
After 50003 training step(s), loss on training batch is 0.132541.
After 50003 training step(s), loss on training batch is 0.128016.
After 50003 training step(s), loss on training batch is 0.119224.
</code></pre><h2 id="&#x8F93;&#x5165;&#x771F;&#x5B9E;&#x56FE;&#x7247;&#xFF0C;&#x8F93;&#x51FA;&#x9884;&#x6D4B;&#x7ED3;&#x679C;">&#x8F93;&#x5165;&#x771F;&#x5B9E;&#x56FE;&#x7247;&#xFF0C;&#x8F93;&#x51FA;&#x9884;&#x6D4B;&#x7ED3;&#x679C;</h2>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-14-15342235292553.jpg" alt=""></p>
<ul>
<li>&#x7F51;&#x7EDC;&#x8F93;&#x5165;&#xFF1A;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;(784 &#x4E2A;&#x50CF;&#x7D20;&#x70B9;)</li>
</ul>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-14-15342235796505.jpg" alt=""></p>
<ul>
<li>&#x50CF;&#x7D20;&#x70B9;&#xFF1A;0-1 &#x4E4B;&#x95F4;&#x7684;&#x6D6E;&#x70B9;&#x6570;(&#x63A5;&#x8FD1; 0 &#x8D8A;&#x9ED1;&#xFF0C;&#x63A5;&#x8FD1; 1 &#x8D8A;&#x767D;)</li>
</ul>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-14-15342236301350.jpg" alt=""></p>
<ul>
<li><p>&#x7F51;&#x7EDC;&#x8F93;&#x51FA;&#xFF1A;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;(&#x5341;&#x4E2A;&#x53EF;&#x80FD;&#x6027;&#x6982;&#x7387;)&#xFF0C;&#x6570;&#x7EC4;&#x4E2D;&#x6700;&#x5927;&#x7684;&#x90A3;&#x4E2A;&#x5143;&#x7D20;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x7D22; &#x5F15;&#x53F7;&#x5C31;&#x662F;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
</li>
<li><p>&#x5173;&#x952E;&#x5904;&#x7406;&#xFF1A;</p>
</li>
</ul>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">application</span><span class="hljs-params">()</span>:</span>
  testNum = input(<span class="hljs-string">&quot;input the number of test pictures:&quot;</span>) 
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(testNum):
    testPic = raw_input(<span class="hljs-string">&quot;the path of test picture:&quot;</span>)
    testPicArr = pre_pic(testPic)
    preValue = restore_model(testPicArr)
    print(<span class="hljs-string">&quot;The prediction number is:&quot;</span>, preValue)
</code></pre>
<p>&#x6CE8;&#x89E3;:
&#x4EFB;&#x52A1;&#x5206;&#x6210;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x5B8C;&#x6210;
1)<code>testPicArr = pre_pic(testPic)</code>&#x5BF9;&#x624B;&#x5199;&#x6570;&#x5B57;&#x56FE;&#x7247;&#x505A;&#x9884;&#x5904;&#x7406;
2)<code>preValue = restore_model(testPicArr)</code>&#x5C06;&#x7B26;&#x5408;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x8F93;&#x5165;&#x8981;&#x6C42;&#x7684;&#x56FE;&#x7247;&#x5582;&#x7ED9;&#x590D;&#x73B0;&#x7684;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#xFF0C;&#x8F93;&#x51FA;&#x9884;&#x6D4B;&#x503C;</p>
<ul>
<li>&#x5177;&#x4F53;&#x4EE3;&#x7801;&#xFF1A;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">restore_model</span><span class="hljs-params">(testPicArr)</span>:</span>
  <span class="hljs-comment">#&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x56FE;&#xFF0C;&#x5728;&#x8BE5;&#x56FE;&#x4E2D;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x64CD;&#x4F5C;&#xFF08;&#x591A;&#x6570;&#x64CD;&#x4F5C;&#x548C;train&#x4E2D;&#x4E00;&#x6837;&#xFF0C;&#x5C31;&#x4E0D;&#x518D;&#x91CD;&#x590D;&#x4EE3;&#x7801;&#x4E86;&#xFF0C;&#x5BF9;&#x7167;&#x5B66;&#x4E60;&#x5373;&#x53EF;&#xFF09;</span>
  <span class="hljs-keyword">with</span> tf.Graph().as_default() <span class="hljs-keyword">as</span> tg:
    x = tf.placeholder(tf.float32, [<span class="hljs-keyword">None</span>, mnist_forward.INPUT_NODE])
    y = mnist_forward.forward(x, <span class="hljs-keyword">None</span>)
    preValue = tf.argmax(y, <span class="hljs-number">1</span>) <span class="hljs-comment"># &#x5F97;&#x5230;&#x6982;&#x7387;&#x6700;&#x5927;&#x7684;&#x9884;&#x6D4B;&#x503C;</span>

    <span class="hljs-comment"># &#x5B9E;&#x73B0;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x6A21;&#x578B;&#xFF0C;&#x53C2;&#x6570;MOVING_AVERAGE_DECAY&#x7528;&#x4E8E;&#x63A7;&#x5236;&#x6A21;&#x578B;&#x66F4;&#x65B0;&#x7684;&#x901F;&#x5EA6;&#xFF0C;&#x8BAD;&#x7EC3;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F1A;&#x5BF9;&#x6BCF;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x7EF4;&#x62A4;&#x4E00;&#x4E2A;&#x5F71;&#x5B50;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x4E2A;&#x5F71;&#x5B50;&#x53D8;&#x91CF;&#x7684;&#x521D;&#x59CB;&#x503C;</span>
    <span class="hljs-comment"># &#x5C31;&#x662F;&#x76F8;&#x5E94;&#x53D8;&#x91CF;&#x7684;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x6BCF;&#x6B21;&#x53D8;&#x91CF;&#x66F4;&#x65B0;&#x65F6;&#xFF0C;&#x5F71;&#x5B50;&#x53D8;&#x91CF;&#x5C31;&#x4F1A;&#x968F;&#x4E4B;&#x66F4;&#x65B0; </span>
    variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
    variables_to_restore = avriable_averages.variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)
    <span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
      <span class="hljs-comment"># &#x901A;&#x8FC7;checkpoint&#x6587;&#x4EF6;&#x5B9A;&#x4F4D;&#x5230;&#x6700;&#x65B0;&#x4FDD;&#x5B58;&#x7684;&#x6A21;&#x578B;</span>
      ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
      <span class="hljs-keyword">if</span> ckpt <span class="hljs-keyword">and</span> ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        preValue = sess.run(preValue, feed_dict={x:   testPicArr  })
        <span class="hljs-keyword">return</span> preValue
      <span class="hljs-keyword">else</span>:
        print(<span class="hljs-string">&quot;No checkpoint file found&quot;</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>

<span class="hljs-comment">#&#x9884;&#x5904;&#x7406;&#x51FD;&#x6570;&#xFF0C;&#x5305;&#x62EC;`resize`&#xFF0C;&#x8F6C;&#x53D8;&#x7070;&#x5EA6;&#x56FE;&#xFF0C;&#x4E8C;&#x503C;&#x5316;&#x64CD;&#x4F5C;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pre_pic</span><span class="hljs-params">(picName)</span>:</span>
  img = Image.open(picName)
  reIm = img.resize((<span class="hljs-number">28</span>,<span class="hljs-number">28</span>), Image.ANTIALIAS)
  im_arr = np.array(reIm.convert(<span class="hljs-string">&apos;L&apos;</span>))
  threshold = <span class="hljs-number">50</span> <span class="hljs-comment">#&#x8BBE;&#x5B9A;&#x5408;&#x7406;&#x7684;&#x9608;&#x503C;</span>
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">28</span>):
    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">28</span>):
      im_arr[i][j] = <span class="hljs-number">255</span> - im_arr[i][j]
      <span class="hljs-keyword">if</span> (im_arr[i][j] &lt; threshold):
        im_arr[i][j] = <span class="hljs-number">0</span>
      <span class="hljs-keyword">else</span>:
        im_arr[i][j] = <span class="hljs-number">255</span>
  nm_arr = im_arr.reshape([<span class="hljs-number">1</span>, <span class="hljs-number">784</span>])
  nm_arr = nm_arr.astype(np.float32)
  img_ready = np.multiply(nm_arr, <span class="hljs-number">1.0</span>/<span class="hljs-number">255.0</span>)
  <span class="hljs-keyword">return</span> img_ready
</code></pre>
<p>1&#x3001;&#x6CE8;&#x89E3;:
1)<code>main</code>&#x51FD;&#x6570;&#x4E2D;&#x7684;<code>application</code>&#x51FD;&#x6570;&#xFF1A;&#x8F93;&#x5165;&#x8981;&#x8BC6;&#x522B;&#x7684;&#x51E0;&#x5F20;&#x56FE;&#x7247;(&#x6CE8;&#x610F;&#x8981;&#x7ED9;&#x51FA;&#x5F85;&#x8BC6; &#x522B;&#x56FE;&#x7247;&#x7684;&#x8DEF;&#x5F84;&#x548C;&#x540D;&#x79F0;)&#x3002;
2)&#x4EE3;&#x7801;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#xFF1A; </p>
<p>(1)&#x6A21;&#x578B;&#x7684;&#x8981;&#x6C42;&#x662F;&#x9ED1;&#x5E95;&#x767D;&#x5B57;&#xFF0C;&#x4F46;&#x8F93;&#x5165;&#x7684;&#x56FE;&#x662F;&#x767D;&#x5E95;&#x9ED1;&#x5B57;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x5BF9;&#x6BCF;&#x4E2A;&#x50CF;&#x7D20;&#x70B9;&#x7684;&#x503C;&#x6539;&#x4E3A;255 &#x51CF;&#x53BB;&#x539F;&#x503C;&#x4EE5;&#x5F97;&#x5230;&#x4E92;&#x8865;&#x7684;&#x53CD;&#x8272;&#x3002; </p>
<p>(2)&#x5BF9;&#x56FE;&#x7247;&#x505A;&#x4E8C;&#x503C;&#x5316;&#x5904;&#x7406;(&#x8FD9;&#x6837;&#x4EE5;&#x6EE4;&#x6389;&#x566A;&#x58F0;&#xFF0C;&#x53E6;&#x5916;&#x8C03;&#x8BD5;&#x4E2D;&#x53EF;&#x9002;&#x5F53;&#x8C03;&#x8282;&#x9608;&#x503C;)&#x3002;</p>
<p>(3)&#x628A;&#x56FE;&#x7247;&#x5F62;&#x72B6;&#x62C9;&#x6210;1&#x884C;784&#x5217;&#xFF0C;&#x5E76;&#x628A;&#x503C;&#x53D8;&#x4E3A;&#x6D6E;&#x70B9;&#x578B;(&#x56E0;&#x4E3A;&#x8981;&#x6C42;&#x50CF;&#x7D20;&#x70B9;&#x662F;0-1&#x4E4B;&#x95F4;&#x7684;&#x6D6E;&#x70B9;&#x6570;)&#x3002;</p>
<p>(4)&#x63A5;&#x7740;&#x8BA9;&#x73B0;&#x6709;&#x7684;<code>RGB</code>&#x56FE;&#x4ECE;<code>0-255</code>&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x53D8;&#x4E3A;<code>0-1</code>&#x4E4B;&#x95F4;&#x7684;&#x6D6E;&#x70B9;&#x6570;&#x3002; </p>
<p>(5)&#x8FD0;&#x884C;&#x5B8C;&#x6210;&#x540E;&#x8FD4;&#x56DE;&#x5230;<code>main</code>&#x51FD;&#x6570;&#x3002;</p>
<p>(6)&#x8BA1;&#x7B97;&#x6C42;&#x5F97;&#x8F93;&#x51FA; y&#xFF0C;y &#x7684;&#x6700;&#x5927;&#x503C;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x5217;&#x8868;&#x7D22;&#x5F15;&#x53F7;&#x5C31;&#x662F;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-08-18 15:53:44
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: 第四章 全连接网络实践">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="section4.2.html" class="navigation navigation-next " aria-label="Next page: 第二节 制作数据集">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第一节 输入手写数字图片输出识别结果","level":"1.5.1","depth":2,"next":{"title":"第二节 制作数据集","level":"1.5.2","depth":2,"path":"chapter4/section4.2.md","ref":"chapter4/section4.2.md","articles":[]},"previous":{"title":"第四章 全连接网络实践","level":"1.5","depth":1,"path":"chapter4/README.md","ref":"chapter4/README.md","articles":[{"title":"第一节 输入手写数字图片输出识别结果","level":"1.5.1","depth":2,"path":"chapter4/section4.1.md","ref":"chapter4/section4.1.md","articles":[]},{"title":"第二节 制作数据集","level":"1.5.2","depth":2,"path":"chapter4/section4.2.md","ref":"chapter4/section4.2.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter4/section4.1.md","mtime":"2018-08-18T07:53:44.137Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

